import signal, os
import time

def handler_signal(signum, frame):
    print("signal handler called with signal", signum)
    raise OSError("error: task timeout")

if __name__ == "__main__":
    signal.signal(signal.SIGALRM, handler_signal)
    signal.alarm(5)

    # 模拟一个超时任务，如果5秒内没有完成，则抛出异常
    # fd = os.open("/dev/rand", os.O_RDWR)
    count = 10
    while count > 0:
        print("count:",count)
        time.sleep(1)
        count -= 1

    signal.alarm(0)  # 取消定时器
